package com.travel.sight.web;

import com.alibaba.fastjson.JSON;
import com.travel.common.vo.MemberRespVo;
import com.travel.sight.entity.CommentEntity;
import com.travel.sight.service.CommentService;
import com.travel.sight.vo.CommentVo;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import static com.travel.common.constant.AuthServerConstant.LOGIN_USER;

@Controller
@Slf4j
public class CommentWebController {

    @Autowired
    CommentService commentService;

    @Autowired
    StringRedisTemplate redisTemplate;



    /**
     * 保存评论
     * @return
     */
    @PostMapping("/saveComment")
    public String saveComment(@RequestParam("content")String content,
                              @RequestParam("score")Integer score,
                              @RequestParam(value = "imageFile",required = false) MultipartFile imageFile,  //可以不上传图片
                              @RequestParam("attractionId")Integer attractionId) throws Exception {

        //1. 从redis中取出用户信息
        String s = redisTemplate.opsForValue().get(LOGIN_USER);
        MemberRespVo memberRespVo = JSON.parseObject(s,MemberRespVo.class);
        Integer userId = memberRespVo.getMemberId().intValue();

        //2. 保存评论
        commentService.saveComment(content,score,imageFile,attractionId,userId);

        //3. 评论后的一系列更新操作
        commentService.updateAfterComment(attractionId,userId);

        return "redirect:http://sight.travel.com/"+attractionId+".html";
    }
}
